<!-- 
	介绍：该页面为微信小程序用户授权登录页面是一个过度页面，微信公众号不需要用户授权获取用户信息则不需要显示具体页面信息
 -->
<template>
	<view>
		<view v-show="isLoading" class="content">
			<view class="zcHeader">
				<view class="posImg">
					<image src="../../static/bg.png" mode="widthFix" style="width: 100%;height: 100%;"></image>
				</view>
				<view class="head_info">
					<view class="head_img">
						<image src="../../static/l1.png" mode="widthFix" style="width: 100%;height: 100%;"></image>
					</view>
					<view>精典数字门店</view>
				</view>
			</view>
			<view class="zcCenter">
				<view class="zcCenter_title">登录授权后将获得以下权限</view>
				<view class="iconfont zcCenter_info">获得你的公开信息（昵称、头像、地区等）</view>
			</view>
			<checkbox-group @change="checkboxChange">
				<label class="cb_lab">
					<checkbox value="1" color="#FFFFFF" style="transform: scale(0.5);" /><text>我已阅读并同意《用户协议》和《隐私政策》</text>
				</label>
			</checkbox-group>
			<button type="primary" :disabled="isDLogin" open-type="getUserInfo" lang="zh_CN" class="zcBut" @getuserinfo="doUserInfo">登录</button>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isDLogin: true,
				isLoading: false,
				isLogin: true,
				type: '',
				imgPath: this.$ZcPublic.Domain
			}
		},
		onLoad(e) {
			let _this = this;
			console.log('页面参数 = ', e);
// uni.setEnableDebug({
// 	enableDebug: true
// })
			// 判断用户操作的是否是注册，值为：register则注册，否则为登录，有上个页面传过来
			_this.type = e.type;

			// #ifdef H5
			// _this.isLoading = true;
			// H5 页面获取 openId ，需要参数 appid（微信公众号的 appId）、
			// action(获取到后台获取到 openID 后跳转相关页面的 参数判定，参数值的结构：[当前操作平台名称  _login] ，_login:是否登录，不传则默认注册) ，
			window.location.href = this.$ZcPublic.Domain + 'WXServer/getOpenId.ashx?appid=' + e.appid + '&action=' + e.action;
			// #endif

			// #ifdef MP-WEIXIN
			let IsCanSysInfo = uni.canIUse('getSystemInfoSync.return.screenWidth');
			if (IsCanSysInfo) {
				let curSystem = uni.getSystemInfoSync();
				console.log('curSystem',curSystem);
				let AnZhuoVer = "8.0.10";
				let IosVer = "8.0.4";
				if(curSystem.system.indexOf("Android")>=0){
					console.log("安卓",curSystem.system);
					let _AnZhuoVer = AnZhuoVer.split('.');
					let curSystemVersion = curSystem.version.split('.');
					let bol = (()=>{
						if(_AnZhuoVer[0]<curSystemVersion[0]){
							return true;
						}else if(_AnZhuoVer[0] == curSystemVersion[0]){
							if(_AnZhuoVer[1]<curSystemVersion[1]){
								return true;
							}else if(_AnZhuoVer[1] == curSystemVersion[1]){
								if(_AnZhuoVer[2]<curSystemVersion[2]){
									return true;
									}
							}
						}
					})();
					// if(bol){
					// 	console.log("安卓微信版本过低");
					// 	uni.navigateTo({
					// 		url: '../replaceVer/replaceVer'
					// 	});
					// 	return
					// }
				}else if(curSystem.system.indexOf("iOS")>=0){
					console.log("ios",curSystem.system);
					let _IosVer = IosVer.split('.');
					let curSystemVersion = curSystem.version.split('.');
					let bol = (()=>{
						if(_IosVer[0]<curSystemVersion[0]){
							return true;
						}else if(_IosVer[0]==curSystemVersion[0]){
							if(_IosVer[1]<curSystemVersion[1]){
								return true;
							}else if(_IosVer[1]==curSystemVersion[1]){
								if(_IosVer[2]<curSystemVersion[2]){
									return true;
									}
							}
						}
					})();
					// 	if(bol){
					// 	console.log("ios微信版本过低");
					// 	uni.navigateTo({
					// 		url: '../replaceVer/replaceVer'
					// 	});
					// 	return
					// }
				}
				}


			// 微信小程序登录、注册，以下是用户已授权获取用户信息的处理
			uni.getSetting({
				success(res) {

					if (res.authSetting['scope.userInfo']) {
						uni.getUserInfo({
							lang: 'zh_CN',
							success(resjson) {
								console.log("已经登陆过检查",resjson);
								console.log('_this.isLogin',_this.isLogin);
								if (!_this.isLogin) {
									return;
								}
								uni.login({
									provider: 'weixin',
									success: function(res1) {
										if (res1.code) {
											uni.showLoading({
												title: '加载中',
												mask: true
											});
											_this.$ZcPublic.Server.Post({
												IsNeedLogin: 'false', //根据需求而定
												Paras: {
													Name: 'Uni_Partner_Detail',
													Action: 'Ext_getUserOpenId',
													// 微信小程序获取 openId 参数详解
													// AppId：微信小程序的 appId,是唯一的，可固定传入
													// jsCode:微信小程序登录后获取到的 jsCode，用于后台获取 openId
													// nickName、avatarUrl、gender、country、province、city、language：用户授权获取到的洪湖信息
													Data: {
														AppId: _this.$ZcPublic.AppId, //wx37eedfa0150a418a（保险）	// wx6244a8ffd9c53b36（众畅）
														jsCode: res1.code,
														nickName: resjson.userInfo.nickName,
														avatarUrl: resjson.userInfo.avatarUrl,
														gender: resjson.userInfo.gender,
														country: resjson.userInfo.country,
														province: resjson.userInfo.province,
														city: resjson.userInfo.city,
														language: resjson.userInfo.language
													}
												},
												Callback: _this.Callback_getLoginOpenId,
												Scope: _this
											})
										}
									}
								});

							}
						})
					} else {
						_this.isLoading = true;
					}
				}
			})
			// #endif


		},
		methods: {
			// 该函数为用户授权获取用户信息是的处理方式，
			doUserInfo(resjson) {
				console.log("查找原因",resjson);
				let _this = this;
				if (resjson.detail.errMsg !== 'getUserInfo:ok') {
					return;
				}
				uni.login({
					provider: 'weixin',
					success: function(res) {
						console.log('code = ', res);
						if (res.code) {
							uni.showLoading({
								title: '加载中'
							});

							_this.$ZcPublic.Server.Post({
								IsNeedLogin: 'false', //根据需求而定
								Paras: {
									Name: 'Uni_Partner_Detail',
									Action: 'Ext_getUserOpenId',
									// UserId: '',//根据需求而定
									// 微信小程序获取 openId 参数详解
									// AppId：微信小程序的 appId,是唯一的，可固定传入
									// jsCode:微信小程序登录后获取到的 jsCode，用于后台获取 openId
									// nickName、avatarUrl、gender、country、province、city、language：用户授权获取到的洪湖信息
									Data: {
										AppId: _this.$ZcPublic.AppId,
										jsCode: res.code,
										nickName: resjson.detail.userInfo.nickName,
										avatarUrl: resjson.detail.userInfo.avatarUrl,
										gender: resjson.detail.userInfo.gender,
										country: resjson.detail.userInfo.country,
										province: resjson.detail.userInfo.province,
										city: resjson.detail.userInfo.city,
										language: resjson.detail.userInfo.language
									}
								},
								Callback: _this.Callback_getLoginOpenId,
								Scope: _this
							})
						}
					}
				});

			},

			// 获取到 openId 登录成功后，通过 回调参数 isLogin跳转处理
			Callback_getLoginOpenId(resjson, scope) {
				console.log('登录回调 = ', resjson);
				let _this = this;
					if (!resjson.success) {
						uni.showModal({
							title: '温馨提示',
							showCancel: false,
							content: '' + resjson.msg
						})

						//uni.clearStorage();
						//uni.clearStorageSync();
						_this.isLoading = true;
						return;
					}else {
						uni.hideLoading();
						if(JSON.stringify(resjson.data).indexOf("对不起，你当前不是销售代表无法处理相关业务，请与管理员联系!")>=0){
								return
							}
							// 登录成功后，将参数存入缓存中，用于后面功能接口登录验证
							uni.setStorageSync('person', resjson.data);
							uni.setStorageSync('key_partner_openid', resjson.data.OpenId);
							uni.setStorageSync('zcsoft_uni_partner_session', resjson.session);
							if (resjson.data.isLogin === 'true') {
								console.log("登录成功");
								uni.switchTab({
									url: '../home/home'
								})
							} else {
								uni.reLaunch({
									url: '../userBind/userBind'
								})
							}
							if (resjson.state === 'err') {
								_this.isLogin = false;
							}
				
					}

			},
			checkboxChange(res) {
				this.isDLogin = !this.isDLogin;
			}
		}
	}
</script>

<style>
	.content {
		position: absolute;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		background-color: #FFFFFF;
		display: flex;
		flex-direction: column;
		/* justify-content: center; */
		align-items: center;
		/* padding: 60upx; */
	}

	.posImg {
		position: relative;
		width: 100%;
	}

	.head_info {
		position: absolute;
		bottom: 0;
	}

	.head_img {
		width: 200upx;
		height: 200upx;
		overflow: hidden;
		border-radius: 20%;
	}

	.head_info view:last-child {
		font-size: 36upx;
		color: #3CA4FA;
		display: flex;
		align-items: center;
		justify-content: center;
		letter-spacing: 6upx;
	}

	.zcHeader {
		display: flex;
		flex-direction: column;
		align-items: center;
		/* padding: 60upx; */
		font-size: 50upx;
		/* font-weight: bold; */
		padding-bottom: 140upx;
		/* border-bottom: 3upx solid #999999; */
		width: 100%;
		box-sizing: border-box;
		position: relative;
	}

	.cb_lab {
		display: flex;
		align-items: center;
		margin-bottom: 40upx;
		font-size: 30upx;
	}

	checkbox-group {
		width: auto !important;
	}

	checkbox[checked] .wx-checkbox-input {
		background-color: #3AA4FA;
	}


	.zcCenter {
		display: flex;
		flex-direction: column;
		padding: 60upx 0 40upx 0;
	}

	.zcCenter_title {
		font-weight: 600;
	}

	.zcCenter_info {
		font-weight: 600;
		color: #999999;
		position: relative;
		padding-left: 25upx;
	}

	.zcCenter_info:before {
		/* content: '\e608'; */
		content: '';
		position: absolute;
		width: 20upx;
		height: 20upx;
		border-radius: 50%;
		left: -10upx;
		top: 15upx;
		background: #3CA4FA;
	}

	.zcBut {
		background-color: #3ca4fa !important;
		padding: 20upx 0;
		font-size: 36upx;
		border-radius: 48upx;
		line-height: 1;
		width: 80%;
		margin-bottom: 40upx;
	}

	button[disabled] {
		background-color: #F5F5F5 !important;
		color: #999999 !important;
	}
</style>
